<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ****************************************************************************
  NAIYUMIE KING WANG JJANG Front End Developer <naiyumie@gmail.com>
  FileType:CRLF, Encording:UTF-8, Tab&Space:4/4
  ------------------------------------------------------------------------
  멤버들
**************************************************************************** */

class MembersM extends N_Model {

	/***************************************************
	 * preprocess
	 *     constructor
	 *     function
	 *     struct and global values
	 ***************************************************/
	var $tableName;
    function __construct() {
        parent::__construct();
    }


	/***************************************************
	 * c
	 ***************************************************/

	# 생성
	function c01($argArray = array()){
		$sqlSetStatment = '';
		$sqlSetStatment = $this->getQuerySetStateMent($argArray);
		$sql = sprintf('
			INSERT INTO
				tbl_members
			SET %1$s;',
			$sqlSetStatment
		);
		$this->db->trans_start();
		$this->db->query($sql, $argArray);
		$this->db->trans_complete();
		if ($this->db->trans_status() === TRUE) {
			$result = TRUE;
		}
		if($result){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}

	# 생성 - 멤버 생성시 예금 레코드 추가
	function c02($argArray = array()){
		$sql01 = sprintF('
			SELECT pk_uid FROM tbl_members WHERE uq_member_id = "%s"',
			$argArray['member_id']
		);
		$result = $this->db->query($sql01)->row_array();
		$argArray['fk_members_uid'] = $result['pk_uid'];
		unset($argArray['member_id']);

		$sqlSetStatment = '';
		$sqlSetStatment = $this->getQuerySetStateMent($argArray);
		$sql = sprintf('
			INSERT INTO tbl_item_deposit_money_members
			SET %1$s',
			$sqlSetStatment
		);
		$this->db->trans_start();
		$this->db->query($sql, $argArray);
		$this->db->trans_complete();
		if ($this->db->trans_status() === TRUE) {
			$result = TRUE;
		}
		if($result){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}


	/***************************************************
	 * r
	 ***************************************************/

	# 목록 조회
	function r01($argArray = array()){
		# 상세
		$whereStatement01 = '';
		if(isset($argArray['pk_uid'])===TRUE){
			$whereStatement01 = sprintf('
				 WHERE pk_uid = %s',
				 $this->db->escape_str($argArray['pk_uid'])
			);
		}

		# 검색
		$whereStatement02 = '';
		$whereKey = '';
		$whereVal = '';
		if(isset($argArray['where'])===TRUE){
			$whereKey = $argArray['where']['whereKey'];
			$whereVal = $argArray['where']['whereVal'];
			if($argArray['where']['whereOpt'] == 'absolute'){
				$whereStatement02 = sprintf('
					WHERE %s = "%s" ',
					$this->db->escape_str($whereKey) , $this->db->escape_str($whereVal)
				);
			} else {
				$whereStatement02 = sprintf('
					WHERE %s LIKE "%%%s%%" ',
					$this->db->escape_str($whereKey) , $this->db->escape_str($whereVal)
				);
			}
		}
		//nlog($whereStatement02);

		# 페이지네이션
		$limitStatement = '';
		if(isset($argArray['paginationCfg']['paginationFirst']) === TRUE && isset($argArray['paginationCfg']['paginationSecond']) === TRUE){
			$limitStatement = sprintf('
				 LIMIT %s, %s',
				 $argArray['paginationCfg']['paginationFirst'], $argArray['paginationCfg']['paginationSecond']
			);
		}
		$orderByStatement = ' ORDER BY pk_uid DESC ';

		# 쿼리
		$sql = sprintf('
			SELECT
				pk_uid,
				uq_member_id,
				pw_member_pw,
				st_name,
				fg_gender,
				(SELECT
					tbl_enum.text
				FROM
					tbl_enum
				WHERE tbl_members.fg_gender = tbl_enum.code) AS gender_kr,
				fg_auth_type,
				(SELECT
					tbl_enum.text
				FROM
					tbl_enum
				WHERE tbl_members.fg_auth_type = tbl_enum.code) AS auth_type_kr,
				dt_joined,
				tm_joined,
				(SELECT
					CONCAT(
						tbl_members.dt_joined," ",tbl_members.tm_joined
					)) AS joined,
				dt_last_logined,
				tm_last_logined,
				(SELECT
					CONCAT(
						tbl_members.dt_last_logined," ",tbl_members.tm_last_logined
					)) AS last_logined,
				fg_terms_of_use,
				(SELECT
					tbl_enum.text
				FROM
					tbl_enum
				WHERE tbl_members.fg_terms_of_use = tbl_enum.code) AS terms_of_use_kr,
				fg_privacy_policy,
				(SELECT
					tbl_enum.text
				FROM
					tbl_enum
				WHERE tbl_members.fg_privacy_policy = tbl_enum.code) AS privacy_policy_kr
			FROM
				tbl_members
			%1$s
			%2$s
			%3$s
			%4$s;',
			$whereStatement01,
			$whereStatement02,
			$orderByStatement,
			$limitStatement
		);
		//nlog($sql);

		# 쿼리실행
		$query = $this->db->query($sql);
		$result = $query->result_array($query);
		if(isset($argArray['pk_uid'])===TRUE){
			$retVal = @$result[0];
		} else {
			$retVal = $result;
		}
		return $retVal;
	}

	# 목록 카운트
	function r02($argArray = array()){
		# 검색조건
		$whereStatement02 = '';
		$whereKey = '';
		$whereVal = '';
		nlog('model argArray', print_r($argArray,true));
		if(isset($argArray['where'])===TRUE){
			$whereKey = $argArray['where']['whereKey'];
			$whereVal = $argArray['where']['whereVal'];
			if($argArray['where']['whereOpt'] == 'absolute'){
				$whereStatement02 = sprintf('
					WHERE %s = "%s" ',
					$this->db->escape_str($whereKey) , $this->db->escape_str($whereVal)
				);
			} else {
				$whereStatement02 = sprintf('
					WHERE %s LIKE "%%%s%%" ',
					$this->db->escape_str($whereKey) , $this->db->escape_str($whereVal)
				);
			}
		}
		//nlog('r02where>>>'+$whereStatement02);

		# 쿼리
		$sql = sprintf('
			SELECT COUNT(pk_uid) AS cnt FROM tbl_members %1$s',
			$whereStatement02
		);
		$query = $this->db->query($sql);
		$result = $query->result_array($query);
		$retVal = @$result[0]['cnt'];
		return $retVal;
	}


	/***************************************************
	 * u
	 ***************************************************/

	# PK를 기준으로 업데이트
	function u01($argArray = array()){
		$sqlSetStatment = '';
		$sqlSetStatment = $this->getQuerySetStateMent($argArray);
		$sql = sprintf('
			UPDATE
				tbl_members
			SET
				%1$s
			WHERE
				pk_uid = %2$s;',
			$sqlSetStatment,
			$this->db->escape_str($argArray['pk_uid'])
		);
		$this->db->trans_start();
		$this->db->query($sql, $argArray);
		$this->db->trans_complete();
		if ($this->db->trans_status() === TRUE) {
			$result = TRUE;
		}
		if($result){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}

	# 로그인시 최종 접속일시 업데이트
	function u02($argArray = array()){
		$sqlSetStatment = '';
		$sql = sprintf('
			UPDATE tbl_members SET
				dt_last_logined = "%1$s",
				tm_last_logined = "%2$s"
			WHERE
				uq_member_id = "%3$s"',
			$argArray['dt_last_logined'],
			$argArray['tm_last_logined'],
			$argArray['uq_member_id']
		);

		$this->db->trans_start();
		$this->db->query($sql, $argArray);
		$this->db->trans_complete();
		if ($this->db->trans_status() === TRUE) {
			$result = TRUE;
		}
		if($result){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}


	/***************************************************
	 * d
	 ***************************************************/

	# PK를 기준으로 삭제
	function d01($argArray = array()){
		if(empty($argArray['pk_uid']) !== FALSE){
			return FALSE;
		}
		$sql = sprintf('
			DELETE FROM
				tbl_members
			WHERE
				pk_uid = %1$s;',
			$this->db->escape_str(@$argArray['pk_uid'])
		);
		//nlog($sql);
		$query = $this->db->query($sql);
		$result = $this->db->affected_rows();
		if($result > 0){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}


}
/* End of file */